define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'spiderrobot_work_sum/index' + location.search,
                    add_url: 'spiderrobot_work_sum/add',
                    // edit_url: 'back_stage_robot_work_sum/edit',
                    // del_url: 'back_stage_robot_work_sum/del',
                    // multi_url: 'back_stage_robot_work_sum/multi',
                    // import_url: 'back_stage_robot_work_sum/import',
                    table: 'spiderrobot_work_sum',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'agentID',
                sortName: 'recordDate',
                fixedColumns: true,
                fixedRightNumber: 1,
                templateView: false,
                clickToSelect: false,
                search: false,
                showColumns: false,
                showToggle: false,
                showExport: false,
                showSearch: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'agentID', title: __('Agent'), searchList: Config.adminUserList,
                            defaultValue: $("#loginUserId").val(),
                        },
                        {field: 'robotUser', title: __('spider robot'), operate: false},
                        {field: 'region', title: __('region'), operate: false},
                        {field: 'IP', title: __('current ip'), operate: false},
                        {
                            field: 'recordDate',
                            title: __('Date'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime,
                            datetimeFormat: 'YYYY-MM-DD'
                        },
                        {field: 'livestreamerNums', title: __('spider host number'), operate: false},
                        {field: 'vipuserNums', title: __('spider vip user number'), operate: false},
                        {field: 'boxDiamonds', title: __('box coin amount'), operate: false},
                        {field: 'recordDate', title: __('Latest update time'), operate: false},
                        {
                            field: 'status', title: __('current state'), formatter: function (value) {
                                if (value == 1)
                                    return "正常";
                                else if (value == 2)
                                    return "异常";
                                else if (value == 9)
                                    return "删除";
                            }, operate: false
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'spidersetting',
                                    text: __('Reset'),
                                    title: __('Reset'),
                                    classname: 'btn btn-xs btn-success btn-dialog btn-spidersetting spidersetting',
                                    refresh: true,
                                    url: "spiderrobot_work_sum/spidersetting"
                                },
                                {
                                    name: 'boxdetail',
                                    text: __('Grab the treasure box'),
                                    title: __('Grab the treasure box'),
                                    classname: 'btn btn-xs btn-success btn-dialog btn-boxdetail boxdetail',
                                    refresh: true,
                                    url: "spiderrobot_work_sum/boxdetail?robotnickname={robotnickname}"
                                },
                                // {
                                //     text: '爬取主播详情',
                                //     title: '爬取主播详情',
                                //     url: "live_streamer/index",
                                //     classname: 'btn btn-xs btn-success',
                                // },
                                {
                                    text: 'VIP',
                                    title: 'VIP',
                                    url: "vip_user/index",
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                }
                            ]
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        spidersetting: function () {
            Controller.api.bindevent();
        },
        boxdetail: function () {
            let ids = location.pathname.split("/").slice(-1);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'spiderrobot_work_sum/boxdetail/ids/' + ids + location.search,
                }
            });


            var table = $("#boxdetail");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'recordTime',
                fixedColumns: true,
                fixedRightNumber: 1,
                templateView: false,
                clickToSelect: false,
                search: false,
                showColumns: false,
                showToggle: false,
                showExport: false,
                showSearch: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'robotUser', title: __('Robot account'), defaultValue: Config.robotnickname},
                        {field: 'recordTime', title: __('Time')},
                        {field: 'livestreamerUser', title: __('Room ID'), operate: false},
                        {field: 'getDiamonds', title: __('The number of diamonds taken'), operate: false},
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
